<template>
  <el-dialog title="添加用户" :visible.sync="isShow" @close="colseAddFrom" :before-close="handleClose">
    <el-form ref="addForm" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="setIsShow()">取 消</el-button>
      <el-button type="primary" @click="addUser()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  //接收父组件传值
  props: ['isShow'],
  data() {
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error('请输入合法的邮箱'))
    }
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if (regMobile.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号'))
    }
    return {
      form: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机', trigger: 'blur' },
          { min: 11, max: 11, message: '长度为11位', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    }
  },
  //向父组件传值
  methods: {
    setIsShow() {
      this.$emit('setIsShow', false)
    },
    colseAddFrom() {
      this.$refs.addForm.resetFields()
    },
    //before-close 仅当用户通过点击关闭图标或遮罩关闭 Dialog 时起效
    handleClose() {
      this.setIsShow()
    },
    addUser() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const { data: res } = await this.$http.post('users', this.form)
          if (res.meta.status === 201) {
            this.$message.success(res.meta.msg)
            //添加成功后通知父组件刷新数据
            this.$emit('Refresh')
          } else {
            this.$message.success(res.meta.msg)
          }
          this.setIsShow()
        }
      })
    }
  }
}
</script>

<style>
</style>